﻿using DataAchive.Model.Excel.Export;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAchive.Extend
{
    public class NAiDefectStaticsReportAllColumn
    {
        public static List<AiDefectStaticColumnAiC> GetSmallReportAids(List<SmallReportRowCol> heBingReport)
        {
            //local 中文 ||英文
            AiDefectStaticColumnAiC aiVl = new AiDefectStaticColumnAiC();


            List<string> allfaults = new List<string>();

            aiVl.ProductCount = heBingReport.Count;
            if (heBingReport.Where(x => !string.IsNullOrWhiteSpace(x.VbAiResult)).Count() > 0)
            {
                if (heBingReport.Where(x => !string.IsNullOrWhiteSpace(x.ElAiResult)).Count() > 0 &&
                    heBingReport.Where(x => !string.IsNullOrWhiteSpace(x.ViAiResult)).Count() > 0)
                {
                    aiVl.OKCount = heBingReport.Count(x => x.VbAiResult != "NG" && x.ElAiResult != "NG" && x.ViAiResult != "NG");
                }
                else if (heBingReport.Where(x => !string.IsNullOrWhiteSpace(x.ElAiResult)).Count() > 0)
                {
                    aiVl.OKCount = heBingReport.Count(x => x.VbAiResult != "NG" && x.ElAiResult != "NG");
                }
                else if (heBingReport.Where(x => !string.IsNullOrWhiteSpace(x.ViAiResult)).Count() > 0)
                {
                    aiVl.OKCount = heBingReport.Count(x => x.VbAiResult != "NG" && x.ViAiResult != "NG");
                }
                else
                {
                    aiVl.OKCount = heBingReport.Count(x => x.VbAiResult != "NG");
                }

            }
            else
            {
                if (heBingReport.Where(x => !string.IsNullOrWhiteSpace(x.ElAiResult)).Count() > 0 &&
                    heBingReport.Where(x => !string.IsNullOrWhiteSpace(x.ViAiResult)).Count() > 0)
                {
                    aiVl.OKCount = heBingReport.Count(x => x.ElAiResult != "NG" && x.ViAiResult != "NG");
                }
                else if(heBingReport.Where(x => !string.IsNullOrWhiteSpace(x.ElAiResult)).Count() > 0)
                {
                    aiVl.OKCount = heBingReport.Count(x => x.ElAiResult != "NG" );
                }
                else
                {
                    aiVl.OKCount = heBingReport.Count(x => x.ViAiResult != "NG");
                }


            }

            aiVl.NGCount = aiVl.ProductCount - aiVl.OKCount;


            Dictionary<string, int> countDictionary = new Dictionary<string, int>();
            foreach (var itemCol in heBingReport)
            {
                var item_el = itemCol.ElAiDefects.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var item_vi = itemCol.ViAiDefects.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var item_vB = itemCol.VbAiDefects.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var elFault in item_el)
                {
                    var fault = elFault.Trim();
                    if (countDictionary.ContainsKey(fault))
                    {
                        countDictionary[fault]++;
                    }
                    else
                    {
                        countDictionary[fault] = 1;
                    }
                }
                foreach (var viFault in item_vi)
                {
                    var fault = viFault.Trim();
                    if (countDictionary.ContainsKey(fault))
                    {
                        countDictionary[fault]++;
                    }
                    else
                    {
                        countDictionary[fault] = 1;
                    }
                }
                foreach (var vbFault in item_vB)
                {
                    var fault = vbFault.Trim();
                    if (countDictionary.ContainsKey(fault))
                    {
                        countDictionary[fault]++;
                    }
                    else
                    {
                        countDictionary[fault] = 1;
                    }
                }
            }

            List<AiDefectStaticColumnAiC> aiCs = new List<AiDefectStaticColumnAiC>();
            foreach (var everyOne in countDictionary)
            {
                AiDefectStaticColumnAiC aiDefect = new AiDefectStaticColumnAiC();
                aiDefect.DefectName = everyOne.Key;
                aiDefect.DefectCount = everyOne.Value;

                //total
                aiDefect.ProductCount = aiVl.ProductCount;
                aiDefect.OKCount = aiVl.OKCount;
                aiDefect.NGCount = aiVl.NGCount;

                aiCs.Add(aiDefect);
            }
            if (countDictionary.Count == 0)
            {
                AiDefectStaticColumnAiC aiDefect = new AiDefectStaticColumnAiC();
             
                //total
                aiDefect.ProductCount = aiVl.ProductCount;
                aiDefect.OKCount = aiVl.OKCount;
                aiDefect.NGCount = aiVl.NGCount;

                aiCs.Add(aiDefect);
            }
            return aiCs;


       
        }
    }
}
